var fs=require('fs');
var problems = [], lib = {};
var __ = require("lodash");
var goodInfos = ["你真棒！","你是小天才啊！","百分百正确！","你太厉害了！","智商超两百了！"];
var badInfos = ["你做错了！","错了，猪头！","错了！","你又错了！","错，大错特错！"];
var wrong = 0, correct = 0;
var runMode = 1;

function varClear(){
    wrong = 0;
    correct = 0;
}

function randClick(){
    varClear();
    runMode = 1;
    $("#problem").val(getProblem());
    $("#infoOut").val("你现在处于随机模式，准备开始学习！");
    $("#inputInfo").focus();
}
function orderClick(){
    varClear();
    runMode = 2;
    $("#problem").val(problems[0]);
    $("#infoOut").val("你现在处于顺序模式，准备开始学习！");
    $("#inputInfo").focus();
}

function getProblem(){
    return problems[__.random(problems.length-1)];
}

function getGoodInfo(){
    return goodInfos[__.random(goodInfos.length-1)];
}

function getBadInfo(){
    return badInfos[__.random(badInfos.length-1)];
}

(function(){
    $("input:radio:first").prop("checked",true);
    var files = fs.readdirSync('./json');
    for(var k = 0; k < files.length; k++){
        if(__.endsWith(files[k],".json"))
            __.extend(lib, JSON.parse(fs.readFileSync("./json/" + files[k])));
    }
    //console.dir(lib);
    var keys = Object.keys(lib);
    for( var i = 0; i < keys.length; i++){
        if(problems.indexOf(lib[keys[i]]) < 0){
            problems.splice(__.random(problems.length-1),0,lib[keys[i]]);
        }
    }
    //console.dir(problems);
    $("#problem").val(getProblem());
    $("#infoOut").val("你现在处于随机模式，准备开始学习！");
    $("#inputInfo").focus();
})();

function answerCommit(){
    var answer,problem;
    if(arguments.length == 0){
        answer = lib[$("#inputInfo").val().trim()];
        problem = $("#problem").val();
    }
    else if(arguments.length == 1){
        answer = arguments[0];
        problem = $("#problem").val().toUpperCase().charCodeAt();
    }
    if(answer==problem){
        correct++;
        $("#infoOut").attr("style","color:green");
        $("#infoOut").val(getGoodInfo() + "\n\n做对" + correct + "题；做错" + wrong + "题。\n\n正确率：" +
                  (correct/(wrong+correct)*100).toFixed(2)+"% 。");
    }else{
        wrong++;
        $("#infoOut").attr("style","color:red");
        $("#infoOut").val(getBadInfo() + "\n\n做对" + correct + "题；做错" + wrong + "题。\n\n正确率：" +
                  (correct/(wrong+correct)*100).toFixed(2)+"% 。");
    }

    if(runMode == 1){
        $("#problem").val(getProblem());
    }else{
        if(wrong+correct==problems.length){
            $("input:radio:first").prop("checked",true);
            alert("恭喜你，任务完成！\n\n题目："+(wrong+correct)+"道，正确："+correct+"道，正确率："+
                  (correct/(wrong+correct)*100).toFixed(2)+"% 。");
            randClick();
        } else{
            $("#problem").val(problems[wrong+correct]);
        }
    }
    $("#inputInfo").focus();
    $("#inputInfo").val("");
}

function keyUp(e) {
    var ev= window.event||e;
    if (ev.keyCode == 13 && $("#inputInfo").val().trim().length > 0) {
        answerCommit();
    }else if(ev.keyCode == $("#problem").val().toUpperCase().charCodeAt()){
        answerCommit(ev.keyCode);
    }
}
